﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.IO;
using System.Windows.Forms;
using DAL;
using Entities;

namespace BL
{
    public class TransaccionesBLL
    {
        public static void GrabarStockMovimientos(DataSet dtStockMov, DataRowView drvw, string origen, ref int? codigoError)
        {
            MySqlTransaction tr = null;
            try
            {
                MySqlConnection SqlConnection1 = DALBase.GetConnection();
                SqlConnection1.Open();
                tr = SqlConnection1.BeginTransaction();
                DAL.StockMovDAL.GrabarDB(dtStockMov, drvw, SqlConnection1, tr);
                DAL.StockMovDetalleDAL.GrabarDB(dtStockMov, SqlConnection1, tr);
                tr.Commit();
                SqlConnection1.Close();
            }
            catch (MySqlException ex)
            {
                if (ex.Number == 1042) //no se pudo abrir la conexion por falta de internet
                {
                    dtStockMov.RejectChanges();
                    codigoError = 1042;
                }
                else
                {
                    dtStockMov.RejectChanges();
                    if (tr != null)
                    {
                        tr.Rollback();
                    }
                    codigoError = ex.Number;
                }
            }
        }

        public static void GrabarVentas(DataSet dtVentas, DataRowView drvw, ref int? codigoError)
        {
            MySqlTransaction tr = null;
            try
            {
                MySqlConnection SqlConnection1 = DALBase.GetConnection();
                SqlConnection1.Open();
                tr = SqlConnection1.BeginTransaction();
                DAL.VentasDAL.GrabarDB(dtVentas, drvw, SqlConnection1, tr);
                DAL.VentasDetalleDAL.GrabarDB(dtVentas, SqlConnection1, tr);
                tr.Commit();
                SqlConnection1.Close();
            }
            catch (MySqlException ex)
            {
                if (ex.Number == 1042) //no se pudo abrir la conexion por falta de internet
                {
                    dtVentas.RejectChanges();
                    codigoError = 1042;
                }
                else
                {
                    dtVentas.RejectChanges();
                    if (tr != null)
                    {
                        tr.Rollback();
                    }
                    codigoError = ex.Number;
                }
            }
        }
    }
}
